<?php
declare(strict_types=1);

namespace app\listener;

use app\event\UserInvited;
use app\event\DistributionOrderCreated;
use app\model\MessageTemplate;
use app\service\MessageService;

class DistributionNotify
{
    protected $messageService;
    
    public function __construct(MessageService $messageService)
    {
        $this->messageService = $messageService;
    }

    /**
     * 处理用户邀请成功通知
     */
    public function onUserInvited(UserInvited $event)
    {
        // 获取邀请人信息
        $inviter = \app\model\User::find($event->inviterId);
        $invitee = \app\model\User::find($event->inviteeId);
        
        // 发送通知
        $this->messageService->send(
            $event->merchantId,
            $event->inviterId,
            'distribution_invite_success',
            [
                'invitee_name' => $invitee->nickname ?: $invitee->mobile,
                'time' => date('Y-m-d H:i:s')
            ]
        );
    }
    
    /**
     * 处理分销订单创建通知
     */
    public function onDistributionOrderCreated(DistributionOrderCreated $event)
    {
        // 获取订单信息
        $order = \app\model\Order::find($event->orderId);
        
        // 发送通知
        $this->messageService->send(
            $event->merchantId,
            $event->parentId,
            'distribution_order_created',
            [
                'order_no' => $event->orderNo,
                'commission' => $event->commission,
                'time' => date('Y-m-d H:i:s')
            ]
        );
    }
} 